<?php
/**
 * Created by PhpStorm.
 * User: shx
 * Date: 2016/11/29
 * Time: 10:36
 */

namespace App\Http\Controllers\Tag;

use App\Model\Category;
use App\Models\Live\Anchor;
use App\Models\Tag\Tag;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class LiveController extends Controller
{
    public function index(Request $request)
    {
        $query = Anchor::leftJoin('lives', 'anchors.live_id', '=', 'lives.id')
            ->select(
                ['anchors.status'
                    ,'lives.name as live_name'
                    ,'anchors.name as name'
                    ,'anchors.id as id'
                    ,'anchors.online as online'
                    ,'anchors.live_id as live_id'
                    ,'anchors.category_id as category_id'
                ]
            )
            ->orderBy('anchors.status', 'desc')
            ->orderBy('anchors.online', 'desc')
        ;

        if ($request->has('s')) {
            $search = trim($request->input('s'));

            if (is_numeric($search)) {
                $anchor = Tag::where('id', $search)->first();
                if ($anchor) $search = $anchor->name;
            }

            $search = preg_replace('/[\x{10000}-\x{10FFFF}\s\?]/u', '', $search);
            $search = preg_replace('/[\?\%\*]/u', '\\\$0', $search);
            $query->where('anchors.name', 'like', "%{$search}%");
        }

        if ($request->has('live') && DB::table('lives')->where('id', $request->input('live'))->first()) {
            $query->where('anchors.live_id', $request->input('live'));
        }

        if ($request->has('cate')) {
            $query->ofCate($request->input('cate'));
        }

        $count = (clone $query);

        return view('tag.lives')
            ->with('online', $count->where('anchors.status', 1)->count())
            ->with('anchors', $query->paginate(20)->appends($request->only(['s', 'live', 'cate'])))
            ->with('lives', DB::table('lives')->get())
            ->with('cates', Category::where('cate_pid', 47)->orderBy('cate_weight', 'asc')->get())
            ;
    }


    public function edit(Request $request, $anchor_id)
    {
        $anchor = Anchor::findOrFail($anchor_id);

        $tag = Tag::where('anchor_id', $anchor_id)->first();

        if (!$tag) {
            $tag = Tag::create([
                'name' => $anchor->name,
                'type' => 'anchor',
                'anchor_id' => $anchor_id
            ]);
        }

        return redirect()->route('tag.edit', [$tag->id]);
    }

    public function download($count)
    {
        $count = (int) $count;
        if ($count > 20000) $count = 20000;
        if ($count < 5000) $count = 5000;
        header('Content-Type: plain/text');
        header( "Content-Disposition: attachment; filename=主播数据.txt ");
        $ids = Anchor::orderBy('online', 'desc')->take($count)->whereNotIn('live_id', [1,5,9])->get();
        foreach ($ids as $v){
            echo ($v->tag()->id ? route('anchor.show', [$v->tag()->id]) : route('anchor.preview', [$v->id]))."\r\n";
        }
        exit();
    }
}